﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace IterationLife.Extensions.Sms
{
    public class LuosimaoSmsSender : ISmsSender
    {
        public string Suffix { get; set; } = "【迭代生活】";

        private const string SendUrl = "http://sms-api.luosimao.com/v1/send.json";

        private static readonly string AuthrizationKey = "key-c63ecbf88cf0efdb3270ce8f0bb34818";

        private static readonly string AuthorizationToken;

        static LuosimaoSmsSender()
        {
            AuthorizationToken = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes($"api:{AuthrizationKey}"));
        }

        public void SendSms(string phone, string message)
        {
            message = message + Suffix;

            var parameters = new Dictionary<string, string>();
            parameters.Add("mobile", phone);
            parameters.Add("message", message);
            using (HttpClient client = new HttpClient())
            {
               
                var content = new FormUrlEncodedContent(parameters);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, SendUrl);
                request.Content = content;
                request.Headers.Add("Authorization",AuthorizationToken);
                var response = client.SendAsync(request).GetAwaiter().GetResult();
                var stringResult = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
            }
        }
    }
}
